package org.hoyi.WebRender;

import java.util.HashMap;
import java.util.Map;

/**
 * Hoyi的JS配置。主要用于内置的CDN.JS，以免每个页面都写一堆JS引用.
 * 待开发功能：系统配置内置JS或内容.
 * @author sam
 *
 */
public class HoyiJSConfig {

	private static HoyiJSConfig instance;

	public static HoyiJSConfig getInstance() {
		if (instance == null) {
			instance = new HoyiJSConfig();
			instance.InitURLS();
		}
		return instance;
	}

	public void InitURLS() {
		JqueryURLs.put("1.9.1", "https://libs.baidu.com/jquery/1.9.1/jquery.min.js");
		JqueryURLs.put("2.1.3", "https://libs.baidu.com/jquery/2.1.3/jquery.min.js");
	}

	/**
	 * 如果未设置多版本的时候，可以直接从defJqueryURL里面获取
	 */
	public String defJqueryURL = "https://libs.baidu.com/jquery/2.1.3/jquery.min.js";
	
	/**
	 * 获取Jquery验证.
	 * @return 返回参数.
	 */
	public String GetJqueryValiLibs() {
		return "<script src=\"https://cdn.bootcss.com/jquery-validate/1.18.0/jquery.validate.js\"></script>\n"
				+ "<script src=\"https://cdn.bootcss.com/jquery-validate/1.18.0/localization/messages_zh.js\"></script>\n"
				+ ""
				;
	}
	/**
	 * BootStrapLib
	 * @return 返回参数.
	 */
	public String GetBootStrapLibs() {
		return  "		<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->\n" + "" 
	+ "		<link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css\" integrity=\"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u\" crossorigin=\"anonymous\">\n" 
	+
	"		<!-- 可选的 Bootstrap 主题文件（一般不用引入） -->\n" + 
	"		<link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css\" integrity=\"sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp\" crossorigin=\"anonymous\">\n" + 
	"" + 
	"		<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->\n" + 
	"		<script src=\"https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js\" integrity=\"sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa\" crossorigin=\"anonymous\"></script>\n"
	+ ""
	;
	}
	
	/**
	 * hoyi-req, hoyi-server-req
	 * 
	 * // 这里输出HOYI的JS．
	// 输出hoyi-req.js
	// 输出hoyi-req-serverpost-v.3.15.js
	 * @return 返回参数.
	 */
	public String GetHoyiReqLib() {
		return "		<script typet=\"text/javascript\">function HOYI_POSTFA(b,c,a){$.ajax({cache:true,async:false,type:\"POST\",data:b,url:\"?\",error:function(d){if(a!=null){a(d)}alert(\"Connection error\")},success:function(e,d){if(d==\"success\"){c(e,d)}else{alert(\"execute error\")}}})}function HOYI_POST(b,c,a){delparsent(b);$.ajax({cache:true,async:true,type:\"POST\",data:b,url:\"?\",error:function(d){if(a!=null){a(d)}alert(\"Connection error\")},success:function(e,d){if(d==\"success\"){c(e,d)}else{alert(\"execute error\")}}})}function HOYI_POST_BEhavior(b,d,a){var c={\"behavior\":b};$.ajax({cache:true,async:true,type:\"POST\",data:c,url:\"?\",error:function(e){if(a!=null){a(e)}alert(\"Connection error\")},success:function(f,e){if(e==\"success\"){d(f,e)}else{alert(\"execute error\")}}})}function HOYI_POST_HEHAVI_DATA(b,c,d,a){$.ajax({cache:true,async:true,type:\"POST\",data:c,url:\"?behavior=\"+b,error:function(e){if(a!=null){a(e)}alert(\"Connection error\")},success:function(f,e){if(e==\"success\"){d(f,e)}else{alert(\"execute error\")}}})}function HOYI_POST_URL(a,b,c){delparsent(b);$.ajax({cache:true,async:true,type:\"POST\",url:a,data:b,error:function(d){alert(\"Connection error\")},success:function(e,d){if(d==\"success\"){c(e,d)}else{alert(\"execute error\")}}})}function HOYI_POST_FULL(a,c,d,b){$.ajax({cache:true,async:true,type:\"POST\",url:a,data:c,error:function(e){alert(\"Connection error\")},success:function(f,e){if(e==\"success\"){d(f,e)}else{if(b!=null){b(f,e)}alert(\"execute error\")}}})}function HOYI_validateForm(b,a){return $(formid).validate({submitHandler:function(c){if(a!=null){a()}}}).form()}function HOYI_POST_HAVEPARSENT(b,c,a){$.ajax({cache:true,async:true,type:\"POST\",data:b,url:\"?\",error:function(d){if(a!=null){a(d)}alert(\"Connection error\")},success:function(e,d){if(d==\"success\"){c(e,d)}else{alert(\"execute error\")}}})}function delparsent(b){for(var a in b){try{b[a]=b[a].replace(/%/g,\"%25\")}catch(c){}}};</script>\n"
				+"		<script typet=\"text/javascript\">function getUrlParam(a){var b=new RegExp(\"(^|&)\"+a+\"=([^&]*)(&|$)\");var c=window.location.search.substr(1).match(b);if(c!=null){return unescape(c[2])}return null}function validateForm(a){return $(a).validate({submitHandler:function(b){}}).form()}function Decrypk(c){var b=HoyipRd.split(\";\")[0];var f=HoyipRd.split(\";\")[1];var e=new RSAUtils.getKeyPair(f,\"\",b);var d=c.split(\"\").reverse().join(\"\");var a=RSAUtils.encryptedString(e,d);return a}function call(functionName,rdata){eval(\"this.\"+functionName+\"(\"+rdata+\")\")}function InitHoyiClick(){InitHoyiClickVPar(\"*\")}function InitHoyiClickVPar(vparid){var parcontents=\"*\";if(vparid!=null&&vparid!=\"\"){parcontents=vparid}$(parcontents+\"[ServerCtrl='true']\").each(function(idx,item){$(item).unbind(\"click\").click(function(){var _OnBeforeClick=$(this).attr(\"OnBeforeClick\");if(_OnBeforeClick!=null){var ret=eval(_OnBeforeClick);if(ret==false){return}}var pjson={};var putdata={};var currentindex=null;for(var ees in HOYICLICK_EVENTID){if(HOYICLICK_EVENTID[ees].eventid==$(this).attr(\"OnHoyiClick\")){currentindex=ees;pjson=HOYICLICK_EVENTID[ees].params}}for(var key in pjson){putdata[key]=$(pjson[key]).val()}var pjson1={};for(var aas in HOYIPOST_EVENTPAMS){if(HOYIPOST_EVENTPAMS[aas].eventid==$(this).attr(\"OnHoyiClick\")){currentindex=aas;pjson1=HOYIPOST_EVENTPAMS[aas].params}}var hoyiseced=null;var hoyiurldecoded=null;for(var i=0;i<pjson1.length;i++){hoyiseced=$(\"#\"+pjson1[i]).attr(\"HoyiSeced\");hoyiurldecoded=$(\"#\"+pjson1[i]).attr(\"HoyiUDecode\");if(hoyiseced==true||hoyiseced==\"true\"){var password=$(\"#\"+pjson1[i]).val();var encrypedPwd=Decrypk(password);putdata[pjson1[i]]=encrypedPwd}else{if(hoyiurldecoded==false||hoyiurldecoded==\"false\"){var password=$(\"#\"+pjson1[i]).val();var encrypedPwd=password.replace(/%/g,\"%25\");putdata[pjson1[i]]=encrypedPwd}else{var urldpwd=$(\"#\"+pjson1[i]).val();var encryurlpwd=encodeURIComponent(urldpwd);putdata[pjson1[i]]=encryurlpwd}}}var pdata={\"behavior\":$(this).attr(\"OnHoyiClick\"),};$.extend(pdata,putdata);HOYI_POST_HAVEPARSENT(pdata,function(rdata,status){eval(rdata)})})})}$(function(){InitHoyiClick()});</script>\n"
				+ "";
	}
	/**
	 * BootStrap Validator
	 * @return 返回参数.
	 */
	public String GetBootStraVali() {
		return "		<link href=\"https://cdn.bootcss.com/bootstrap-validator/0.5.3/css/bootstrapValidator.min.css\" rel=\"stylesheet\">\n" + 
				"		<script src=\"https://cdn.bootcss.com/bootstrap-validator/0.5.3/js/bootstrapValidator.min.js\"></script>\n"
				+ "";
	}

	/**
	 * 包含所有的JQUEYR地址， MAP<Version, URL>,
	 * 
	 * 当前系统会默认添加公网的JQUERY地址， 如果自己想设置的话，可以在系统启动的时候将当前Map Clear掉，然后初始化自己的路径就可以了.
	 */
	public Map<String, String> JqueryURLs = new HashMap<>();

	/**
	 * 根据版本号返回Jquery.cdn.引用.
	 * 
	 * @param version 版本号
	 * @return GetJqueryByVarsion
	 */
	public String GetJqueryByVarsion(String version) {
		return JqueryURLs.get(version);
	}

}
